/*** Class Name: IfulfillQueryEmailHandlerTest
 * Author: Accenture
 * Date: 30-sep-2011
 * Requirement/Project Name: iFulfill
 * Requirement Description: Test class for IfulfillQueryEmailHandler
 **/

@istest
private  class IfulfillQueryEmailHandlerTest
{

 /*
  *  Test method 
  **/
  static testMethod void queryEmailServTest() 
  {
  
     Profile pro = [select id from profile where name='System Administrator'];
     User usr = new User(alias = 'sysad', email='sysadmin@accenture.com',emailencodingkey='UTF-8', lastname='Tes12', languagelocalekey='en_US',localesidkey='en_US', profileid = pro.Id,timezonesidkey='Asia/Calcutta', username='testad123@testorg.com');
     Insert usr;     
  
     Messaging.InboundEmail email = new Messaging.InboundEmail() ;
     Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
     email.subject = 'Query name Q-00005';
     env.fromAddress = 'user@acme.com';
     Id OORSalesRepRecordType = Schema.SObjectType.Queries__c.getRecordTypeInfosByName().get('OOR Sales Rep').getRecordTypeId();   
     Fullfilment_Item__c fi = new Fullfilment_Item__c(   Main_Listing_Phone_Number__c = '100',Listed_Name__c ='XYZ',Heading_Name__c ='Heading name',Item_Id__c = 'Item id' ,Product_Code__c ='p1',UDAC__c ='Udac1',Region__c = 'region1',Transaction_Type__c ='New');
     insert fi;
     Fullfilment_Item__c  tempFI = [select ownerID  from Fullfilment_Item__c  where id = :fi.id ];
     tempFI.ownerID = usr.id;
     update tempFI;
      
     Query_Request__c qr = new Query_Request__c();
     qr.Fulfillment_Item__c = FI.id;
     qr.Request_Reason__c= 'Reason 1';
     insert qr;
     
     Query_Request__c tempQr = [select id from   Query_Request__c where id = :qr.id];
     Queries__c qry = new Queries__c(RecordTypeID=OORSalesRepRecordType,Time_Queried__c=11,Override_Sales_Rep__c = false);       
     qry.Query_Request_ID__c =  tempQr.id;
     insert qry;
      // add an attachment
      Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
      attachment.body = blob.valueOf('my attachment text');
      attachment.fileName = 'textfile.txt';
      attachment.mimeTypeSubType = 'text/plain';
 
      email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };
     
      Messaging.InboundEmail.TextAttachment textAttachment = new Messaging.InboundEmail.TextAttachment();
      textAttachment .body ='my attachment text';
      textAttachment .fileName = 'textfile.txt';
      textAttachment .mimeTypeSubType = 'text/plain';
 
      email.textAttachments= new Messaging.inboundEmail.TextAttachment[] { textAttachment };
      
     // Call the class and test it with the data in the test method.
     IfulfillQueryEmailHandler queryEmailHandler = new IfulfillQueryEmailHandler();
     queryEmailHandler .handleInboundEmail(email, env );
     email.subject = 'Query name-';
     email.plainTextBody = 'For system use only, DO NOT DELETE Query- Q-00005';
     queryEmailHandler .handleInboundEmail(email, env ); 
   
     list<Queries__c> fiList = [Select name from Queries__c where ID = :qry.id];
     if(fiList.size() > 0)
     {
         email.subject = 'Query name-';
         email.plainTextBody = 'For system use only, DO NOT DELETE Query -' + fiList.get(0).name;
         queryEmailHandler.handleInboundEmail(email, env );
     }  
   
  }
}